package org.jeecg.modules.qyxxora.controller;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.fk.utils.CheckRoleUtil;
import org.jeecg.modules.qyxxora.entity.ZbjkScyszbjcMx;
import org.jeecg.modules.qyxxora.entity.ZbjkScyszbjcQy;
import org.jeecg.modules.qyxxora.service.IZbjkScyszbjcMxService;
import org.jeecg.modules.qyxxora.vo.*;
import org.jeecg.modules.qyxxora.service.IZbjkScyszbjcQyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;

import org.jeecg.modules.sssj.view.MyJeecgTemplateExcelView;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.def.TemplateExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.TemplateExportParams;
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 生产要素指标监测企业
 * @Author: jeecg-boot
 * @Date:   2021-12-23
 * @Version: V1.0
 */
@Slf4j
@Api(tags="生产要素指标监测企业")
@RestController
@RequestMapping("/qyxx/zbjkScyszbjcQy")
public class ZbjkScyszbjcQyController extends JeecgController<ZbjkScyszbjcQy, IZbjkScyszbjcQyService> {
	@Autowired
	private IZbjkScyszbjcQyService zbjkScyszbjcQyService;
	@Autowired
	private IZbjkScyszbjcMxService zbjkScyszbjcMxService;
	@Autowired
	private ISysUserService sysUserService;

	/**
	 * 分页列表查询
	 *
	 * @param zbjkScyszbjcQy
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "生产要素指标监测企业-分页列表查询")
	@ApiOperation(value="生产要素指标监测企业-分页列表查询", notes="生产要素指标监测企业-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ZbjkScyszbjcQy zbjkScyszbjcQy,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ZbjkScyszbjcQy> queryWrapper = QueryGenerator.initQueryWrapper(zbjkScyszbjcQy, req.getParameterMap());
		Page<ZbjkScyszbjcQy> page = new Page<ZbjkScyszbjcQy>(pageNo, pageSize);
		IPage<ZbjkScyszbjcQy> pageList = zbjkScyszbjcQyService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  * 分页列表查询
	  *
	  * @param queryVo
	  * @return
	  */
	 @AutoLog(value = "生产要素指标监测企业并统计各预警数-分页列表查询")
	 @ApiOperation(value="生产要素指标监测企业并统计各预警数-分页列表查询", notes="生产要素指标监测企业并统计各预警数-分页列表查询")
	 @GetMapping(value = "/listQy")
	 public Result<?> queryPageListQy(ScysZbjcQueryVo queryVo,
									  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		 Page page = new Page<ZbjkScyszbjcQy>(queryVo.getPageNo(), queryVo.getPageSize());
		 LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 Set<String> roleNameListString = sysUserService.getUserRolesSet(loginUser.getUsername());
		 //判断是否为部门领导，若是则添加部门前缀作为查询条件
		 if (CheckRoleUtil.checkIsDeptAdmin(roleNameListString)) {
			 //查询本部门或下属部门
			 if (StringUtils.isBlank(queryVo.getOrgCode()) || !queryVo.getOrgCode().startsWith(loginUser.getOrgCode())) {
				 queryVo.setOrgCode(loginUser.getOrgCode());
			 }
		 } else if (!CheckRoleUtil.checkIsSysAdmin(roleNameListString)) {
			 //不是部门领导，也不是管理员，则只能查询管户
			 queryVo.setManageUserId(loginUser.getId());
		 }

		 //客户经理：queryVo.setManageUserId();
		 IPage<ScysZbjcPageResultVo> pageList = zbjkScyszbjcQyService.selectScyczbjcPage(page, queryVo);
		 return Result.OK(pageList);
	 }

	 /**
	  * 生产要素指标监测企业-汇总
	  *
	  * @param queryVo
	  * @return
	  */
	 @AutoLog(value = "生产要素指标监测企业-汇总")
	 @ApiOperation(value="生产要素指标监测企业-汇总", notes="生产要素指标监测企业-汇总")
	 @GetMapping(value = "/countQy")
	 public Result<ScysZbjcCountResultVo> countQy(ScysZbjcQueryVo queryVo) {
		 LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 Set<String> roleNameListString = sysUserService.getUserRolesSet(loginUser.getUsername());
		 //判断是否为部门领导，若是则添加部门前缀作为查询条件
		 if (CheckRoleUtil.checkIsDeptAdmin(roleNameListString)) {
			 //查询本部门或下属部门
			 if (StringUtils.isBlank(queryVo.getOrgCode()) || !queryVo.getOrgCode().startsWith(loginUser.getOrgCode())) {
				 queryVo.setOrgCode(loginUser.getOrgCode());
			 }
		 } else if (!CheckRoleUtil.checkIsSysAdmin(roleNameListString)) {
			 //不是部门领导，也不是管理员，则只能查询管户
			 queryVo.setManageUserId(loginUser.getId());
		 }

		 ScysZbjcCountResultVo scysZbjcCountResultVo = zbjkScyszbjcQyService.selectScysjcQyCount(queryVo);
		 return Result.OK(scysZbjcCountResultVo);
	 }

	/**
	 * 添加
	 *
	 * @param zbjkScyszbjcQy
	 * @return
	 */
	@AutoLog(value = "生产要素指标监测企业-添加")
	@ApiOperation(value="生产要素指标监测企业-添加", notes="生产要素指标监测企业-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ZbjkScyszbjcQy zbjkScyszbjcQy) {
		zbjkScyszbjcQyService.save(zbjkScyszbjcQy);
		return Result.OK("添加成功！");
	}

	/**
	 * 编辑
	 *
	 * @param zbjkScyszbjcQy
	 * @return
	 */
	@AutoLog(value = "生产要素指标监测企业-编辑")
	@ApiOperation(value="生产要素指标监测企业-编辑", notes="生产要素指标监测企业-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ZbjkScyszbjcQy zbjkScyszbjcQy) {
		zbjkScyszbjcQyService.updateById(zbjkScyszbjcQy);
		return Result.OK("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "生产要素指标监测企业-通过id删除")
	@ApiOperation(value="生产要素指标监测企业-通过id删除", notes="生产要素指标监测企业-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		zbjkScyszbjcQyService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "生产要素指标监测企业-批量删除")
	@ApiOperation(value="生产要素指标监测企业-批量删除", notes="生产要素指标监测企业-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.zbjkScyszbjcQyService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "生产要素指标监测企业-通过id查询")
	@ApiOperation(value="生产要素指标监测企业-通过id查询", notes="生产要素指标监测企业-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		ZbjkScyszbjcQy zbjkScyszbjcQy = zbjkScyszbjcQyService.getById(id);
		return Result.OK(zbjkScyszbjcQy);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param queryVo
   */
  @RequiresPermissions("ealryWarning:export")
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, ScysZbjcQueryVo queryVo) {
	  LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
	  Set<String> roleNameListString = sysUserService.getUserRolesSet(loginUser.getUsername());
	  //判断是否为部门领导，若是则添加部门前缀作为查询条件
	  if (CheckRoleUtil.checkIsDeptAdmin(roleNameListString)) {
		  //查询本部门或下属部门
		  queryVo.setOrgCode(loginUser.getOrgCode());
	  } else if (!CheckRoleUtil.checkIsSysAdmin(roleNameListString)) {
		  //不是部门领导，也不是管理员，则只能查询管户
		  queryVo.setManageUserId(loginUser.getId());
	  }
	  List<ScysZbjcPageResultVo> pageList = zbjkScyszbjcQyService.selectScyczbjcList(queryVo);
	  List<ScysZbjcPageResultExportVo> exportList = new ArrayList<>();
	  for (ScysZbjcPageResultVo vo : pageList) {
		  LambdaQueryWrapper<ZbjkScyszbjcMx> mxQueryWrapper = Wrappers.lambdaQuery(new ZbjkScyszbjcMx().setDjxh(vo.getDjxh()).setSsny(vo.getSsny()));
		  List<ZbjkScyszbjcMx> list = zbjkScyszbjcMxService.list(mxQueryWrapper);
		  List<ZbjkScyszbjcMxExportVo> mxList = new ArrayList<>();
		  for (ZbjkScyszbjcMx mx : list) {
			  ZbjkScyszbjcMxExportVo mxExportVo = new ZbjkScyszbjcMxExportVo();
			  BeanUtils.copyProperties(mx, mxExportVo);
			  mxList.add(mxExportVo);
		  }
		  ScysZbjcPageResultExportVo exportVo = new ScysZbjcPageResultExportVo();
		  BeanUtils.copyProperties(vo, exportVo);
		  exportVo.setMxList(mxList);
		  exportList.add(exportVo);
	  }

	  ExportParams exportParams=new ExportParams("生产要素指标监测企业报表", "导出人:" + loginUser.getRealname(), "title+i,upLoadPath");
	  exportParams.setType(ExcelType.HSSF);
	  ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
	  mv.addObject(NormalExcelConstants.FILE_NAME, "生产要素指标监测企业"); //此处设置的filename无效 ,前端会重更新设置一下
	  mv.addObject(NormalExcelConstants.CLASS, ScysZbjcPageResultExportVo.class);
	  mv.addObject(NormalExcelConstants.PARAMS,exportParams);
	  mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
	  return mv;
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, ZbjkScyszbjcQy.class);
  }

}
